//
//  SNKSettingCell.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/15.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKSettingCell.h"
#import "SNKSettingItem.h"
#import "SNKSettingArrowItem.h"
#import "SNKSettingSwitchItem.h"
#import "SNKSettingLabelItem.h"
#import <DKNightVersion/DKNightVersion.h>

@interface SNKSettingCell ()

@property(nonatomic,strong)UIImageView *myArrow;
@property(nonatomic,strong)UISwitch *mySwitch;
@property(nonatomic,strong)UILabel *myLabel;

@end

@implementation SNKSettingCell

- (UIImageView *)myArrow {
    if (_myArrow == nil) {
        _myArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellArrow"]];
    }
    return _myArrow;
}

- (UISwitch *)mySwitch {
    if (_mySwitch == nil) {
        _mySwitch = [[UISwitch alloc] init];
        [_mySwitch addTarget:self action:@selector(switchChange) forControlEvents:UIControlEventValueChanged];
    }
    return _mySwitch;
}

-(void)switchChange
{
    //存储开关的状态
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:self.mySwitch.isOn forKey:self.item.title];
    [defaults synchronize];
    
    if (self.mySwitch.isOn) {
        // 切换为夜间模式
        [self.dk_manager nightFalling];
    }else{
        // 切换为白天模式
        [self.dk_manager dawnComing];
    }
}

- (UILabel *)myLabel {
    if (_myLabel == nil) {
        _myLabel = [[UILabel alloc] init];
        _myLabel.bounds = CGRectMake(0, 0, 50, 20);
    }
    return _myLabel;
}

- (void)switchchange
{
    //存储开关的状态

}

// 返回一个cell
+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"setting";
    SNKSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (cell == nil) {
        cell = [[SNKSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:237/255.0 green:233/255.0 blue:218/255.0 alpha:1];
        self.selectedBackgroundView = view;
    }
    return self;
}

- (void)setItem:(SNKSettingItem *)item {
    _item = item;
    
    // 设置cell
    [self setupCell];
    
    // 设置cell右边内容
    [self setupRight];
}

-(void)setupCell
{
    if (self.item.icon) {
        self.imageView.image = [UIImage imageNamed:self.item.icon];
    }
    
    self.textLabel.text = self.item.title;
    self.detailTextLabel.text = self.item.subTitle;
    self.detailTextLabel.textColor = [UIColor redColor];
}

-(void)setupRight
{
    if ([self.item isKindOfClass:[SNKSettingArrowItem class]]) //箭头
    {
        self.accessoryView = self.myArrow;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        
    }else if([self.item isKindOfClass:[SNKSettingSwitchItem class]])//开关
    {
        self.accessoryView = self.mySwitch;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // 设置开关的状态
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        self.mySwitch.on = [defaults boolForKey:self.item.title];
        
        
    }else if([self.item isKindOfClass:[SNKSettingLabelItem class]])//标签
    {
        self.accessoryView = self.myArrow;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
        
    }else
    {
        self.accessoryView = nil;
        self.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
}

- (void)setFrame:(CGRect)frame {
    
    frame.size.height -= 1;
    [super setFrame:frame];
}

@end
